// pages/course/course.js
import {
  request
} from '../../utils/util.js'
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    bookList: [],
    page: 1,
    pagesize: 20,
    tab: [{
      text: '候补中',
      nav_type: 1
    }, {
      text: '待上课',
      nav_type: 2
    }, {
      text: '已上课',
      nav_type: 3
    }, {
      text: '已取消',
      nav_type: 4
    }, ],
    tabIndex: 1,
    nav_type: 2,
    isRequest:false
  },

  // 去评价
  toComment: function (e) {
    let { id, teacher_id } = e.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/courseComment/courseComment?id=${id}&teacher_id=${teacher_id}`,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  onShow: function () {
    this.data.page=1
    this.getBookList()
  },

  //取消预约
  cancelBook: function (e) {
    console.log(e)
    let { id,date } = e.currentTarget.dataset;
    let nowTime =  new Date();
    let thisTime =  new Date(date.replace(/-/g,'/'));
    let time = Math.abs((thisTime-nowTime)/60/60/1000);
    console.log(nowTime,thisTime,time);
    let str = time>8?'':'开课八小时内取消会扣课时，';
    wx.showModal({
      title: '温馨提示',
      content: str+'确认要取消预约吗？',
      showCancel: true,
      success: function (res) {
        if (res.confirm) {
          request({
            url: 'User/cancelBook',
            method: 'POST',
            data: { id },
            success: data => {
              wx.showModal({
                title: '温馨提示',
                content: '取消预约成功',
                showCancel: false,
                success: function (res) {
                  wx.startPullDownRefresh()
                },
              })
            }
          })
        }
      },
    })
  },

  // 缺勤点击
  isDuty:function(e){
    let {date}=e.currentTarget.dataset;
    wx.showToast({
      title: '缺勤时间：'+date,
      icon:'none'
    })
  },

  //取消候补
  cancelStandby: function (e) {
    let { id } = e.currentTarget.dataset;
    wx.showModal({
      title: '温馨提示',
      content: '确认要取消候补吗？',
      showCancel: true,
      success: function (res) {
        if (res.confirm) {
          request({
            url: 'User/cancelCandidate',
            method: 'POST',
            data: { time_id: id },
            success: data => {
              wx.showModal({
                title: '温馨提示',
                content: '取消候补成功',
                showCancel: false,
                success: function (res) {
                  wx.startPullDownRefresh();
                },
              })
            }
          })
        }
      },
    })
  },

  // 获取预约列表
  getBookList: function () {
    let { bookList, page, pagesize, nav_type, isRequest } = this.data
    if(!isRequest){
      this.data.isRequest=true
      request({
        url: 'User/userBookCourseTime',
        method: 'GET',
        data: { nav_type, page, pagesize },
        success: data => {
          this.data.isRequest=false
          let tempList = data.course_time_list
          if (tempList.length == 0) {
            if(page==1){
              bookList = []
            }
            wx.showToast({
              title: '没有更多了～',
              icon: 'none'
            })
          } else {
            page++
            bookList = [...(page==2?[]:bookList), ...tempList]
          }
          this.setData({ page, bookList })
        }
      })

    }
  },

  // 切换状态
  tabChange(e) {
    let { index, type } = e.currentTarget.dataset
    this.data.page = 1
    this.setData({ tabIndex: index, nav_type: type, bookList: [] })
    this.getBookList()
  },

  // 下拉刷新
  onPullDownRefresh: function () {
    this.setData({ page: 1, bookList: [] })
    this.getBookList()
    wx.stopPullDownRefresh()
  },

  // 上拉加载
  onReachBottom: function () {
    this.getBookList()
  },
})